# -*- coding:utf-8 -*-
from django.db import models
from django.db.models import CharField
from Apps.common.custommodel import CustomModel


class Menu(CustomModel):
    parent = models.ForeignKey(
        to="Menu",
        on_delete=models.CASCADE,
        verbose_name="上级菜单",
        null=True,
        blank=True,
        db_constraint=False,
        help_text="上级菜单",
    )
    icon = models.CharField(max_length=64, verbose_name="菜单图标", null=True, blank=True, help_text="菜单图标")
    name = models.CharField(max_length=64, verbose_name="菜单名称", help_text="菜单名称")
    sort = models.IntegerField(default=1, verbose_name="显示排序", null=True, blank=True, help_text="显示排序")
    is_link = models.BooleanField(default=False, verbose_name="是否外链", help_text="是否外链")
    # 是目录就是菜单，不是目录就是按钮
    is_directory = models.BooleanField(default=False, verbose_name="是否目录", help_text="是否目录")
    web_path = models.CharField(max_length=128, verbose_name="路由地址", null=True, blank=True, help_text="路由地址")
    component = models.CharField(max_length=128, verbose_name="组件地址", null=True, blank=True, help_text="组件地址")
    component_name = models.CharField(max_length=50, verbose_name="组件名称", null=True, blank=True,
                                      help_text="组件名称")
    status = models.BooleanField(default=True, blank=True, verbose_name="菜单状态", help_text="菜单状态")
    perms = models.CharField(max_length=64, unique=True, verbose_name="权限字符", help_text="权限字符")

    class Meta:
        db_table = "system_menu"
        verbose_name = "菜单表"
        verbose_name_plural = verbose_name
        ordering = ("sort", "-create_datetime")


class Role(CustomModel):
    name = models.CharField(max_length=64, verbose_name="角色名称", help_text="角色名称")
    sort = models.IntegerField(default=1, verbose_name="角色顺序", help_text="角色顺序")
    status = models.BooleanField(default=True, verbose_name="角色状态", help_text="角色状态")
    # 如果为admin，不允许编辑，即默认角色
    admin = models.BooleanField(default=False, verbose_name="是否为admin", help_text="是否为admin")
    menu = models.ManyToManyField(
        to="Menu",
        db_constraint=False,
        help_text="关联菜单",
        verbose_name="关联菜单",
    )

    class Meta:
        db_table = "system_role"
        verbose_name = "角色表"
        verbose_name_plural = verbose_name
        ordering = ("sort", "-create_datetime")


class SystemUserLoginInfo(CustomModel):
    userid = CharField(max_length=150, verbose_name="成员UserID")
    username = CharField(max_length=150, verbose_name='登陆用户名')
    browser = CharField(max_length=64, verbose_name="浏览器")
    ipaddr = CharField(max_length=32, verbose_name="ip地址", null=True, blank=True)
    device = CharField(max_length=64, verbose_name="登陆设备", null=True, blank=True)
    loginLocation = CharField(max_length=64, verbose_name="登录位置", null=True, blank=True)
    msg = CharField(max_length=32, verbose_name="操作信息", null=True, blank=True)
    os = CharField(max_length=32, verbose_name="操作系统", null=True, blank=True)
    login_type = CharField(max_length=16, verbose_name='登陆方式(密码登陆，扫码登陆)')
    status = CharField(max_length=32, verbose_name="登陆状态(登陆成功，登陆失败)")
    losePassword = CharField(max_length=128, verbose_name='登陆失败时，输入的密码')

    class Meta:
        db_table = "systemloginlog"
        verbose_name = '登录日志'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)

    def __str__(self):
        return f"{self.username}"
